# -*- coding: utf-8 -*-
# ==========================
# @Time    : 2023/12/6
# @Author  : zhoutengwei
# @File    : storage.py
# ==========================
from datetime import timedelta, datetime

from minio import Minio, S3Error
from modelscope.utils.logger import get_logger

logger = get_logger()


class MinioStorage:
    def __init__(self, addr, access_key, secret_key):
        self.addr = addr
        self.access_key = access_key
        self.secret_key = secret_key

    def new_session(self):
        return Minio(self.addr,
                     access_key=self.access_key,
                     secret_key=self.secret_key,
                     secure=False)

    def list_objects(self, bucket_name):
        try:
            client = self.new_session()
            objects = client.list_objects(bucket_name)
            print(objects)
        except S3Error as e:
            err_msg = "There was some error when create new s3 session"
            logger.error(f"{err_msg}: {str(e)}", exc_info=True)

    def get_object(self, bucket_name, object_name):
        try:
            client = self.new_session()
            # while :
            response = client.get_object(bucket_name, object_name)
            return response

        except S3Error as e:
            err_msg = f"There was some error when " \
                      f"get object {object_name} in {bucket_name}"
            logger.error(f"{err_msg}: {str(e)}", exc_info=True)

    def put_object(self, bucket_name, object_name, data, mtype):
        try:
            client = self.new_session()
            # while :
            response = client.put_object(bucket_name, object_name, data, length=len(data.getvalue()), content_type=mtype)
            return response

        except S3Error as e:
            err_msg = f"There was some error when " \
                      f"put object {object_name} in {bucket_name}"
            logger.error(f"{err_msg}: {str(e)}", exc_info=True)

    def fput_object(self, bucket_name, object_name, file_name):
        try:
            client = self.new_session()
            # while :
            response = client.fput_object(bucket_name, object_name, file_name)
            return response

        except S3Error as e:
            err_msg = f"There was some error when " \
                      f"get object {object_name} in {bucket_name}"
            logger.error(f"{err_msg}: {str(e)}", exc_info=True)

    def presigned_get_object(self, bucket_name, object_name, days: int):
        try:
            client = self.new_session()
            # while :
            response = client.presigned_get_object(bucket_name, object_name, expires=timedelta(days=days))
            return response

        except S3Error as e:
            err_msg = f"There was some error when " \
                      f"presigned get object {object_name} in {bucket_name}"
            logger.error(f"{err_msg}: {str(e)}", exc_info=True)

class Config:
    addr = "0.0.0.0:9000"
    access_key = 'root'
    secret_key = 'ztw12345'